package com.xhj.sportsnews.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xhj.sportsnews.BR;
import com.xhj.sportsnews.R;
import com.xhj.sportsnews.adapter.HeadineRecyclerAdapter;
import com.xhj.sportsnews.bean.HeadineRight;
import com.xhj.sportsnews.callback.HttpService;
import com.xhj.sportsnews.databinding.FragmentHeadRightBinding;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class HeadRightFragment extends Fragment implements Callback<HeadineRight> {


    private RecyclerView recycler;
    private FragmentHeadRightBinding binding;
    private HeadineRecyclerAdapter<Object> headsHeadineRecyclerAdapter;

    public HeadRightFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_head_right,container,false);
        HashMap<Class<?>, Pair<Integer,Integer>> map = new HashMap<>();
        map.put(HeadineRight.DataBean.SnapshotsBean.class, Pair.create( R.layout.head_right_item,BR.rightrecycleritem));
        map.put(HeadineRight.DataBean.class,Pair.create(R.layout.head_left_item, BR.recycleitem));
        headsHeadineRecyclerAdapter = new HeadineRecyclerAdapter<>(getContext(),new ArrayList<>(),map);
        binding.headRightRecycler.setAdapter(headsHeadineRecyclerAdapter);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getDynamicData().enqueue(this);
    }

    @Override
    public void onResponse(Call<HeadineRight> call, Response<HeadineRight> response) {

        HeadineRight body = response.body();
        Log.d("1620","body.getData().getSnapshots().get(0).getText()");
        headsHeadineRecyclerAdapter.addAll(body.getData().getSnapshots());
    }

    @Override
    public void onFailure(Call<HeadineRight> call, Throwable t) {

    }
}
